package com.patrol.logisticsmanage;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.patrol.logisticsmanage.net.AppConfig;
import com.patrol.logisticsmanage.net.BaseObserver;
import com.patrol.logisticsmanage.net.BaseReponse;
import com.patrol.logisticsmanage.net.BaseRequest;
import com.patrol.logisticsmanage.ui.appear.model.SubCamp;
import com.patrol.logisticsmanage.ui.base.BaseActivity;
import com.patrol.logisticsmanage.ui.base.PickerDialog;
import com.patrol.logisticsmanage.ui.canteen.utils.ToastUtils;

import org.jaaksi.pickerview.dialog.IGlobalDialogCreator;
import org.jaaksi.pickerview.dialog.IPickerDialog;
import org.jaaksi.pickerview.picker.BasePicker;
import org.jaaksi.pickerview.util.Util;
import org.jaaksi.pickerview.widget.DefaultCenterDecoration;
import org.jaaksi.pickerview.widget.PickerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

@Route(path = "/app/main")
public class MainActivity extends BaseActivity {
    public static List<SubCamp> subCampList;
    public static String url1= "",url2= "",url3 = "",url4="",url5="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(navView, navController);
        requestPermission(this);
        subCampList = new ArrayList<>();
        getUrl();
    }

    private void getUrl(){

        BaseRequest.getInstance().getApiServise().getUrl()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<List<SubCamp>>(mContext) {
                    @Override
                    public void onSuccess(BaseReponse<List<SubCamp>> baseReponse) {
                        List<SubCamp> subCamps = baseReponse.getResults();
                        subCampList.addAll(subCamps);
                        for (int i=0;i<subCamps.size();i++){
                            if (TextUtils.equals(subCamps.get(i).getTitle(),"满意度调查")){
                                url1 = subCamps.get(i).getValue();
                            }else if (TextUtils.equals(subCamps.get(i).getTitle(),"公告")){
                                url3 = subCamps.get(i).getValue();
                            }else if (TextUtils.equals(subCamps.get(i).getTitle(),"报修分配任务")){
                                url4 = subCamps.get(i).getValue() + "?username=" + AppConfig.getString("userName");

                            } else if (TextUtils.equals(subCamps.get(i).getTitle(),"通知")){
                                url5 = subCamps.get(i).getValue() + "?username=" + AppConfig.getString("userName");

                            } else {
                                url2 = subCamps.get(i).getValue() + "?username="+AppConfig.getString("userName");
                            }
                        }
                    }

                    @Override
                    public void onCodeError(BaseReponse tBaseReponse) {
                    }

                    @Override
                    public void onFailure(Throwable e, boolean netWork) throws Exception {

                    }
                });
    }


    private void initDefaultPicker() {
        // 利用修改静态默认属性值，快速定制一套满足自己app样式需求的Picker.
        // BasePickerView
        PickerView.sDefaultVisibleItemCount = 3;
        PickerView.sDefaultItemSize = 50;
        PickerView.sDefaultIsCirculation = true;

        // PickerView
        PickerView.sOutTextSize = 18;
        PickerView.sCenterTextSize = 18;
        PickerView.sCenterColor = Color.RED;
        PickerView.sOutColor = Color.GRAY;

        // BasePicker
        int padding = Util.dip2px(this, 20);
        BasePicker.sDefaultPaddingRect = new Rect(padding, padding, padding, padding);
        BasePicker.sDefaultPickerBackgroundColor = Color.WHITE;
        // 自定义弹窗
        BasePicker.sDefaultDialogCreator = new IGlobalDialogCreator() {
            @Override
            public IPickerDialog create(Context context) {
                return new PickerDialog();
            }
        };

        // DefaultCenterDecoration
        DefaultCenterDecoration.sDefaultLineWidth = 1;
        DefaultCenterDecoration.sDefaultLineColor = Color.parseColor("#199475");
        //DefaultCenterDecoration.sDefaultDrawable = new ColorDrawable(Color.WHITE);
        int leftMargin = Util.dip2px(this, 10);
        int topMargin = Util.dip2px(this, 2);
        DefaultCenterDecoration.sDefaultMarginRect =
                new Rect(leftMargin, -topMargin, leftMargin, -topMargin);
    }

    public static void requestPermission(Activity activity) {
        //打开通知栏的权限
        if (JPushInterface.isNotificationEnabled(activity) == 0) {
            new AlertDialog.Builder(activity)
                    .setCancelable(false)
                    .setMessage("通知权限未打开，是否前去打开？")
                    .setPositiveButton("是", (d, w) -> JPushInterface.goToAppNotificationSettings(activity))
                    .setNegativeButton("否", null)
                    .show();
        }
        //申请定位、存储权限
        JPushInterface.requestPermission(activity);
    }

}
